package com.esd.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esd.common.pojo.PageResult;
import com.esd.common.pojo.R;
import com.esd.device.entity.EsdLogDetail;
import com.esd.device.entity.dto.EsdLogSearch;
import com.esd.device.mapper.EsdLogDetailMapper;
import com.esd.device.service.EsdLogDetailService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EsdLogDetailServiceImpl extends ServiceImpl<EsdLogDetailMapper, EsdLogDetail>
        implements EsdLogDetailService {
    @Override
    public void insert(EsdLogDetail esdLogDetail1) {
        baseMapper.insert(esdLogDetail1) ;
    }

    @Override
    public R logSearch(EsdLogSearch esdLogSearch) {
        Page<EsdLogDetail> deviceLogPage = new Page<>(esdLogSearch.getCurrentPage(), esdLogSearch.getPageSize());
        LambdaQueryWrapper<EsdLogDetail> query = new LambdaQueryWrapper<>();
        if (esdLogSearch.getStationId() != null && !esdLogSearch.getStationId().isEmpty()){
            query.like(EsdLogDetail::getStationId, esdLogSearch.getStationId());
        }
        if (esdLogSearch.getStatus() != null && !esdLogSearch.getStatus().isEmpty()){
            query.eq(EsdLogDetail::getStatus, esdLogSearch.getStatus());
        }
        if (esdLogSearch.getLine()!= null && !esdLogSearch.getLine().isEmpty()) {
            query.eq(EsdLogDetail::getLineName, esdLogSearch.getLine());
        }
        if (esdLogSearch.getPosition()!= null && !esdLogSearch.getPosition().isEmpty()) {
            query.like(EsdLogDetail::getPosition, esdLogSearch.getPosition());
        }
        if (esdLogSearch.getDeviceTypeName() != null){
            query.eq(EsdLogDetail::getDeviceTypeName, esdLogSearch.getDeviceTypeName());
        }
        if (esdLogSearch.getChannelName() != null){
            query.eq(EsdLogDetail::getChannelName, esdLogSearch.getChannelName());
        }
        if (esdLogSearch.getTimeScale() != null && esdLogSearch.getTimeScale().length == 2){
            query.between(EsdLogDetail::getMarkTime, esdLogSearch.getTimeScale()[0], esdLogSearch.getTimeScale()[1]);
        }
        query.orderByDesc(EsdLogDetail::getMarkTime);
        Page<EsdLogDetail> esdLogDetailPage = this.page(
                deviceLogPage ,query );
        return R.ok(new PageResult(esdLogDetailPage.getTotal(),esdLogDetailPage.getRecords()));
    }

    @Override
    public void deleteEsdLogDetail() {
        // 获取当前的年份和月份
        int year = LocalDateTime.now().getYear();
        int month = LocalDateTime.now().getMonthValue();
        if (month <= 3){
            year--;
            month += 3;
        }else {
            month -= 3;
        }
        // 使用YearMonth类创建本月的开始YearMonth对象
        LocalDate startDate = LocalDate.of(year, month , 1);
        // 使用LocalDateTime类从YearMonth对象获取本月的开始时间
        LocalDateTime startOfMonth = LocalDateTime.of(startDate, LocalDateTime.MIN.toLocalTime());
        LambdaQueryWrapper<EsdLogDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.le(EsdLogDetail::getMarkTime , startOfMonth) ;
        baseMapper.delete(queryWrapper);
    }

    @Override
    public List<EsdLogDetail> exportData(EsdLogSearch esdLogSearch) {
        LambdaQueryWrapper<EsdLogDetail> query = new LambdaQueryWrapper<>();
        if (esdLogSearch.getStationId() != null && !esdLogSearch.getStationId().isEmpty()){
            query.like(EsdLogDetail::getStationId, esdLogSearch.getStationId());
        }
        if (esdLogSearch.getStatus() != null && !esdLogSearch.getStatus().isEmpty()){
            query.eq(EsdLogDetail::getStatus, esdLogSearch.getStatus());
        }
        if (esdLogSearch.getLine()!= null && !esdLogSearch.getLine().isEmpty()) {
            query.eq(EsdLogDetail::getLineName, esdLogSearch.getLine());
        }
        if (esdLogSearch.getPosition()!= null && !esdLogSearch.getPosition().isEmpty()) {
            query.like(EsdLogDetail::getPosition, esdLogSearch.getPosition());
        }
        if (esdLogSearch.getDeviceTypeName() != null){
            query.eq(EsdLogDetail::getDeviceTypeName, esdLogSearch.getDeviceTypeName());
        }
        if (esdLogSearch.getChannelName() != null){
            query.eq(EsdLogDetail::getChannelName, esdLogSearch.getChannelName());
        }
        if (esdLogSearch.getTimeScale() != null && esdLogSearch.getTimeScale().length == 2){
            query.between(EsdLogDetail::getMarkTime, esdLogSearch.getTimeScale()[0], esdLogSearch.getTimeScale()[1]);
        }
        query.orderByDesc(EsdLogDetail::getMarkTime);
        return baseMapper.selectList(query);
    }
}
